package com.example.teamtribe.main.club.clubintroduce.media;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.base.PhotoActivity;
import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.PopupPics;
import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.database.Media;
import com.example.teamtribe.event.Refresh;
import com.example.teamtribe.utils.Utility;
import com.jph.takephoto.model.TResult;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

public class UpdateMediaActivity extends PhotoActivity implements View.OnClickListener,PopupPics.OnPickListener{
    private Media media;
    private int type;
    private String cover;
    private PopupPics popup;

    private ImageView ivCover;
    private EditText mediaName;
    private EditText mediaDescription;
    private ImageView done;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_addmedia);

        ivCover=(ImageView)findViewById(R.id.iv_cover);
        mediaName=(EditText)findViewById(R.id.et_name);
        mediaDescription=(EditText)findViewById(R.id.et_description);

        ((TextView)findViewById(R.id.tv_title)).setText("编辑相册");
        done=(ImageView)findViewById(R.id.iv_more);
        done.setImageResource(R.drawable.ic_done);
        done.setVisibility(View.VISIBLE);
        done.setOnClickListener(this);
        ivCover.setOnClickListener(this);

        media= getIntent().getParcelableExtra("media");
        type= getIntent().getIntExtra("type",-1);
        if(type==-1)finish();
        Glide.with(this).load(Utility.getUri(media.getCover())).into(ivCover);
        mediaName.setText(media.getName());
        mediaDescription.setText(media.getDescription());
        popup = new PopupPics(this, this);
    }
    
    public static void actionStart(Context context,Media media,int type){
        Intent intent=new Intent(context,UpdateMediaActivity.class);
        intent.putExtra("media",media);
        intent.putExtra("type",type);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cover:
                popup.show(v);
                break;

            case R.id.iv_more:
                String name=mediaName.getText().toString().trim();
                if (TextUtils.isEmpty(name) ) {
                    showToast("请填写名称");
                }else {
                    if (cover != null)
                        upload(media.getRealId(), name, media.getTeamId(), cover, mediaDescription.getText().toString().trim(), type);
                    else
                        updateMedia(media.getRealId(), name, media.getTeamId(), media.getCover(), mediaDescription.getText().toString().trim(), type);
                }
                break;

            default:
                break;
        }
    }
    public void updateMedia(int mediaId,final String name, int teamId, final String cover,final String description,final int type){
        MediaModel.getInstance().updateMedia(mediaId,name, teamId, cover, description, type, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                hideProgress();
                if(type==1) EventBus.getDefault().post(new Refresh(Refresh.ALBUMFRAGMENT));
                else EventBus.getDefault().post(new Refresh(Refresh.VIDEOFRAGMENT));
                Intent intent=new Intent();
                intent.putExtra("mediaName",name);
                intent.putExtra("mediaCover",cover);
                intent.putExtra("mediaDescription",description);
                setResult(RESULT_OK,intent);
                finish();
            }

            @Override
            public void failure(String meg) {
                hideProgress();
                showToast(meg);
            }
        });
    }
    private void upload(final int mediaId,final String name, final int teamId, final String cover, final String describe, final int type) {
        showProgress();
        File file = new File(cover);
        FileModel.getInstance().upload(Utility.getToken(), file, 0, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                updateMedia(mediaId,name, teamId, bundle.getString("fileName"),describe,type);
            }

            @Override
            public void failure(String meg) {
                hideProgress();
                showToast(meg);
            }
        });
    }
//    public void add(int mediaId,String name, int teamId, String cover, String describe, final int type) {
//        MediaModel.getInstance().updateMedia(mediaId,name, teamId, cover, describe, type, new Back() {
//            @Override
//            public void success(@Nullable Bundle bundle) {
//                hideProgress();
//                if(type==1) EventBus.getDefault().post(new Refresh(Refresh.ALBUMFRAGMENT));
//                else EventBus.getDefault().post(new Refresh(Refresh.VIDEOFRAGMENT));
//                finish();
//            }
//
//            @Override
//            public void failure(String meg) {
//                hideProgress();
//                showToast(meg);
//            }
//        });
//    }

    @Override
    public void takeSuccess(TResult result) {
        String path;
        if (result.getImage().isCompressed()) {
            path = result.getImage().getCompressPath();
        } else {
            path = result.getImage().getOriginalPath();
        }
        cover = path;
        Glide.with(this).load(cover).into(ivCover);
    }

    @Override
    public void fromGallery() {
        getTakePhoto().onPickFromGalleryWithCrop(getUri(), getCropOptions());
    }

    @Override
    public void fromCapture() {
        getTakePhoto().onPickFromCaptureWithCrop(getUri(), getCropOptions());
    }
}

